package org.jeecg.modules.job.sub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.job.sub.entity.SubAnswerRecords;
import org.jeecg.modules.job.sub.entity.SubPhaseRecords;
import org.jeecg.modules.job.sub.mapper.SubAnswerRecordsMapper;
import org.jeecg.modules.job.sub.mapper.SubPhaseRecordsMapper;
import org.jeecg.modules.job.sub.service.ISubPhaseRecordsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 阶段答题记录
 * @Author: jeecg-boot
 * @Date:   2022-10-15
 * @Version: V1.0
 */
@Service
public class SubPhaseRecordsServiceImpl extends ServiceImpl<SubPhaseRecordsMapper, SubPhaseRecords> implements ISubPhaseRecordsService {

    @Resource
    private SubAnswerRecordsMapper answerRecordsMapper;
    @Resource
    private ISysBaseAPI sysBaseAPI;

    @Override
    public SubPhaseRecords getPhaseRecord(String userId, String phaseId) {
        return this.getOne(new QueryWrapper<>(new SubPhaseRecords().setPhaseId(phaseId).setUserId(userId)));
    }

    @Override
    public boolean createPhaseRecord(String userId, String phaseId) {
        List<SubAnswerRecords> list=answerRecordsMapper.selectList(new QueryWrapper<>(new SubAnswerRecords().setPhaseId(phaseId).setUserId(userId)));
        int number=list.size();
        int rightNumber=0;
        int errorNumber=0;
        for(SubAnswerRecords item:list){
            if (item.getResult()==1){
                rightNumber++;
            }else{
                errorNumber++;
            }
        }
        //准确率
        BigDecimal rate=new BigDecimal(rightNumber).divide(new BigDecimal(number),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        //获得积分
//        BaseConfig config=configService.getBaseConfig();
//        int integral=config.getQuestionIntegral() * rightNumber;
        SubPhaseRecords records=new SubPhaseRecords();
        SubPhaseRecords result=this.getOne(new QueryWrapper<>(new SubPhaseRecords().setPhaseId(phaseId).setUserId(userId)));
        if (result!=null){
            records.setId(result.getId());
        }
        records.setPhaseId(phaseId);
        records.setUserId(userId);
        records.setRightNumber(rightNumber);
        records.setNumber(number);
//        records.setIntegral(integral);
//        records.setTotalIntegral(config.getQuestionIntegral()*number);
        records.setAccuracyRate(rate);
        this.saveOrUpdate(records);
        if (result==null){
            //添加积分记录
//            BaseIntegral baseIntegral=new BaseIntegral();
//            baseIntegral.setDataId(records.getId());
//            baseIntegral.setIntegral(integral);
//            baseIntegral.setIntegralResource(2);
//            baseIntegral.setUserId(userId);
//            baseIntegral.setIfAdd(1);
//            baseIntegral.setRemark("答题得积分");
//            integralService.addIntegralRecord(sysBaseAPI.getUserById(userId),baseIntegral);
        }
        return true;
    }
}
